#include <iostream>
#include <time.h>
#include <string>
#include <QMessageBox>

#include "logint.h"
#include "util.h"

using namespace std;

LogInt::LogInt (char Msg_Type, string Disp_Msg, string Log_Msg, bool Is_GUI)//If Dispmsg is default then msg will be used for both
{
///Get current time/date
char TimeDateBuff[20];
time_t t=time(NULL);
strftime(TimeDateBuff,20,"%Y-%m-%d %H:%M:%S",localtime(&t));
TimeDate=TimeDateBuff;

///Set variables needed by the object
Displayed=false;
IsGUI=Is_GUI;
MsgType=Msg_Type;
DispMsg=Disp_Msg;
if(Log_Msg.compare(""))
	LogMsg=Disp_Msg;
else
	LogMsg=Log_Msg;

}

LogInt::~LogInt()
{
}

void LogInt::Display()
{

///Check the type of message and whether GUI or CMD and set off who will lunch it
if(MsgType=='E'||MsgType=='e')///Error 
        {
        if(IsGUI==true)
                ErrorGui();
        else
                ErrorCmd();
	Displayed=true;
        }
else if(MsgType=='W'||MsgType=='w')///Warning
        {
        if(IsGUI==true)
                WarningGui();
        else
                WarningCmd();
	Displayed=true;
        }
else if(MsgType=='D'||MsgType=='d')///Debug
	{
        Debug();
	Displayed=true;
	}
else if(MsgType=='P'||MsgType=='p')///Prompt
	{
	if(IsGUI==true)
	        PromptGui();
	else
		PromptCmd();
	Displayed=true;
	}
else
        {
        //TODO:Error in the logInt variable
        }
}

void LogInt::SetOKButtonVisible()///Add okey button to GUI display
{
//TODO: issue warning if ran==true or GUI==false;
	OKButton=true;
}

void LogInt::SetCancelButtonVisible()///Add cancel button to GUI display
//TODO: implement this
{
//TODO: issue warning if ran==true or GUI==false
	CancelButton=true;
}

void LogInt::LogToDB()///Log the message to DB before displaying
{
;
}

void LogInt::ErrorCmd()///error cmd interface
{
cout<<"Error: "<<DispMsg<<endl;
LogToDB();
}

void LogInt::ErrorGui()///error gui interface
{
string Msg="Error: ";
Msg+=DispMsg;
QMessageBox MsgBox;
QString ErrorMsgGui=QString::fromStdString(Msg);
MsgBox.setText(ErrorMsgGui);
MsgBox.exec();
LogToDB();
}

void LogInt::WarningCmd()///warning cmd interface
{
cout<<"Warning: "<<DispMsg<<endl;
LogToDB();
}

void LogInt::WarningGui()///warning gui interface
{
string Msg="Warning: ";
Msg+=DispMsg;
QMessageBox MsgBox;
QString WarningMsgGui=QString::fromStdString(Msg);
MsgBox.setText(WarningMsgGui);
MsgBox.exec();
LogToDB();
}

void LogInt::PromptCmd()///prompt cmd interface
{
cout<<DispMsg<<endl;
LogToDB();
}

void LogInt::PromptGui()///prompt gui interface
{
QMessageBox MsgBox;
QString PromptMsgGui=QString::fromStdString(DispMsg);
MsgBox.setText(PromptMsgGui);
MsgBox.exec();
}

void LogInt::Debug()///debug has no interface just log data
{
LogToDB();
}
